public class Solution988 { 
    
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    
    String max="";
    
    public String smallestFromLeaf(TreeNode root) {
        dfs(root,new StringBuilder());
        return max;
    }

    private void dfs(TreeNode root, StringBuilder stringBuilder) {
        if (root==null){
            return;
        }
        stringBuilder.insert(0,(char)(root.val+'a'));
        if (root.left==null && root.right==null){
            if (max.equals("") || stringBuilder.toString().compareTo(max)<0){
                max=stringBuilder.toString();
            }
        }else {
            dfs(root.left,stringBuilder);
            dfs(root.right,stringBuilder);
        }
        stringBuilder.deleteCharAt(0);
    }

    public static void main(String[] args) {
        System.out.println(new Solution988().smallestFromLeaf(new TreeNode(0,new TreeNode(1,new TreeNode(3),new TreeNode(4)),new TreeNode(2,new TreeNode(3),new TreeNode(4)))));
    }
}
